package cn.jiahua.test15;

import java.util.concurrent.*;

public class Test2 {
    public static void main(String[] args) {
        //自定义线程池
        ExecutorService executorPool= new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors(),
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());  //拒绝策略，不理这个人还抛出异常

        try {
            for (int i = 0; i < 100; i++) {
                //使用线程池，使用线程池创建线程
                executorPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "ok");
                });

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //线程池用完了，程序结束，关闭线程池
            executorPool.shutdown();
        }

    }

}
